package com.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpClientDemo {

	private static String url = "http://10.80.41.19:8080/Wobbia/LoginServlet";

	public static void main(String[] args) {
		// Create an instance of HttpClient.
		HttpClient client = new DefaultHttpClient();

		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("userName", "value1"));
		formparams.add(new BasicNameValuePair("password", "value1"));
		UrlEncodedFormEntity entity = null;
		try {
			entity = new UrlEncodedFormEntity(formparams, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpPost httppost = new HttpPost(url);
		httppost.setEntity(entity);
		
		try {
			// Execute the method.
			HttpResponse response  = client.execute(httppost);

			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + response.getStatusLine());
			}

			// Read the response body.
			HttpEntity resEntity = response.getEntity();
			System.out.println(EntityUtils.toString(resEntity));

		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			client.getConnectionManager().shutdown();
		}
	}
}
